package com.logistic.action;


import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;

import com.logistic.common.Arith;
import com.logistic.domain.Integral;
import com.logistic.service.IntegralService;
import com.opensymphony.xwork2.ActionSupport;

public class IntegralAction extends ActionSupport {

	private Log log = LogFactory.getFactory().getLog(getClass());
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private IntegralService integralService;
	
	public String accoutModify(){
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession(true);
		//该登录用户的ID
		String Uid = (String) session.getAttribute("Uid");
		//支付宝返回过来的多少钱
		String totalFee = request.getParameter("totalFee");
		
		Integral in = new Integral();
		in.setUid(Uid);
		in = integralService.getIntegralByUid(in);
		Double currentMoney = Double.parseDouble(in.getaccoutMoney());
		Double addMoney =  Double.parseDouble(totalFee);
		currentMoney = Arith.add(currentMoney,addMoney);
		//currentMoney = currentMoney+addMoney;
		in.setaccoutMoney(Double.toString(currentMoney));
		
		integralService.modify(in);
		return "alipaySuccess";
	}
	
	public void modify(String uid,String point,String usedPoints,String accoutMoney){
		Integral in = new Integral();
		in.setPoint(point);
		in.setUid(uid);
		in.setUsedPoints(usedPoints);
		in.setaccoutMoney(accoutMoney);
		integralService.modify(in);
	}
	
	public String getIntegralByUid(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String uid = (String) request.getSession().getAttribute("Uid");
		Integral in = new Integral(); 
		in.setUid(uid);
		in = integralService.getIntegralByUid(in);
		request.setAttribute("integralTO", in);
		return "getIngegralSuccess";
	}
	
	public void getIntegralPoint(){
		
		HttpServletRequest request = ServletActionContext.getRequest();
		String uid = request.getParameter("Uid");
		Integral in = new Integral(); 
		in.setUid(uid);
		in = integralService.getIntegralByUid(in);
		HttpServletResponse response = ServletActionContext.getResponse();
        response.setContentType( "text/html" ); 
	    response.setCharacterEncoding( "UTF-8" );
	     
	    try {
	    	if(in==null)
	    	{
	    		log.error("The parameter Uid is "+uid+", and current login people uid is "+ request.getSession(true).getAttribute("Uid"));
	    	}
	    	else
	    	{
	    		response.getWriter().print("{success:true,point:"+in.getPoint()+"}");
	    	}
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	
	

	public IntegralService getIntegralService() {
		return integralService;
	}

	public void setIntegralService(IntegralService integralService) {
		this.integralService = integralService;
	}
	
}
